import {createRouter,createWebHashHistory,RouteRecordRaw} from 'vue-router'
import pinia from '../store/pinia'
import { userStore } from '../store/user'
const store = userStore(pinia)
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ 
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3,// 初始化时的最小百分比,
})
const routes:Array<RouteRecordRaw> = [
   {
        path: "/",
        redirect: "/home"
    },
    {
        path:"/home",
        component: () => import("@/views/Home.vue"),
        name: "Home"
    },{
        path: "/admin",
        component: () => import("@/views/Admin/Admin.vue"),
        children: [
           {
                path:"",
                component: () => import("@/views/Admin/AdminIndex.vue")
            },
            {
                path: "data",
                component: () => import("@/views/Admin/ManageData.vue")
            },
            {
                path: "state",
                component: () => import("@/views/Admin/State.vue")
            },{
                path: "watch",
                component: () => import("@/views/Admin/Watch.vue")
            },{
                path: "user",
                component: () => import("@/views/Admin/User.vue"),
            },{
                path: "analysis",
                component: () => import("@/views/Admin/analysis.vue")
            },{
                path: "setting",
                component: () => import("@/views/Admin/Setting.vue")
            },
            {
                path: "message",
                component: () => import("@/views/Admin/Message.vue")
            }
        ]
    },
    {
        path: "/login",
        component: () => import("@/views/Login.vue"),
        name: "Login"
    }
]


const router = createRouter({
    routes,
    history: createWebHashHistory()
})
// 路由守卫
router.beforeEach(async (to,from,next) => {
    NProgress.start()
    if (to.fullPath !== '/login') {
        if (store.isLogin) return await next() 
        await next("/login")
    } else {
        await next()
    }  
})

router.afterEach(() => {
    NProgress.done()
})
export default router